const jwt = require('jsonwebtoken')
const whiteList = [
	'/api/login',
	'/api/register',
	'/api/login-mouth',
	'/api/oauth/redirect',
	'/api/nodeEmail',
	'/api/comments',
	'/api/blogsList',
	'/api/blogsDetail',
	'/api/search-mouth'
]
module.exports = () => {
	return async (ctx, next) => {
		console.log(ctx.path, '-----------------详情接口')
		if (whiteList.includes(ctx.path)) {
			await next()
		} else {
			try {
				console.log(ctx.request.headers, 'ctx.request.headers')
				// const { token } = ctx.request.headers
				const { authorization } = ctx.request.headers
				console.log(authorization, 'Authorization-----------')
				const data = jwt.verify(authorization, '1904A')
				console.log('data:我在中间件中', data)
				ctx.state.userInfo = data
				await next()
			} catch (err) {
				ctx.body = {
					code: 401,
					data: err
				}
			}
		}
	}
}
